package com.qdsg.ysg.doctor.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.hjq.shape.view.ShapeTextView;
import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseDialogFragment;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.qdsg.ysg.doctor.util.UtilString;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


/**
 * Description
 * Created by lbj on 2022/4/8 10:37
 */

public class AddCallPersonDialog extends BaseDialogFragment {


    private Dialog mDialog;
    private OnItemClickListener mListener;
    private String mName, mPhone, contactName1, contactPhone1, contactName2, contactPhone2, contactName3, contactPhone3;
    private int number;


    public static AddCallPersonDialog newInstance(int number, String contactName1, String contactPhone1, String contactName2, String contactPhone2, String contactName3, String contactPhone3) {
        Bundle args = new Bundle();
        args.putInt( "number", number );
        args.putString( "contactName1", contactName1 );
        args.putString( "contactPhone1", contactPhone1 );
        args.putString( "contactName2", contactName2 );
        args.putString( "contactPhone2", contactPhone2 );
        args.putString( "contactName3", contactName3 );
        args.putString( "contactPhone3", contactPhone3 );

        AddCallPersonDialog fragment = new AddCallPersonDialog();
        fragment.setArguments( args );
        return fragment;
    }


    @SuppressLint("WrongViewCast")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.dialog_add_call, container );
        assert getArguments() != null;
        number = getArguments().getInt( "number" );
        contactName1 = getArguments().getString( "contactName1" );
        contactPhone1 = getArguments().getString( "contactPhone1" );
        contactName2 = getArguments().getString( "contactName2" );
        contactPhone2 = getArguments().getString( "contactPhone2" );
        contactName3 = getArguments().getString( "contactName3" );
        contactPhone3 = getArguments().getString( "contactPhone3" );
        ImageView iv_cancel = view.findViewById( R.id.iv_cancel );
        EditText et_call_name = view.findViewById( R.id.et_call_name );
        EditText et_phone = view.findViewById( R.id.et_phone );
        ShapeTextView tv_sure = view.findViewById( R.id.tv_sure );
        iv_cancel.setOnClickListener( v -> {
            if (mListener != null) {
                mListener.onCancel();
                dismiss();
            }
        } );
        tv_sure.setOnClickListener( v -> {
            if (UtilString.isEmpty( et_call_name.getText().toString().trim() )) {
                ToastUtil.showToastCenter( mContext, "联系人姓名不能为空" );
                return;
            }
            if (UtilString.isEmpty( et_phone.getText().toString().trim() ) || !UtilString.isPhoneNumNo( et_phone.getText().toString().trim() )) {
                ToastUtil.showToastCenter( mContext, "请输入正确的手机号" );
                return;
            }
            if (number == 1) {
                contactName1 = et_call_name.getText().toString().trim();
                contactPhone1 = et_phone.getText().toString().trim();
            } else if (number == 2) {
                contactName2 = et_call_name.getText().toString().trim();
                contactPhone2 = et_phone.getText().toString().trim();
            } else if (number == 3) {
                contactName3 = et_call_name.getText().toString().trim();
                contactPhone3 = et_phone.getText().toString().trim();
            }
            addContact();

        } );

        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );


    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog( savedInstanceState );
        mDialog.setCancelable( false );
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity( Gravity.BOTTOM );
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Objects.requireNonNull( Objects.requireNonNull( getDialog() ).getWindow() ).requestFeature( Window.FEATURE_NO_TITLE );
        super.onActivityCreated( savedInstanceState );
        Objects.requireNonNull( getDialog().getWindow() ).setBackgroundDrawable( new ColorDrawable( 0x00000000 ) );
        getDialog().getWindow().setLayout( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT );
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm();

        void onCancel();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }

    private void addContact() {
        showProgressDialog( mContext );
        Map<String, Object> map = new HashMap<>();
        map.put( "contactName1", contactName1 );
        map.put( "contactPhone1", contactPhone1 );
        map.put( "contactName2", contactName2 );
        map.put( "contactPhone2", contactPhone2 );
        map.put( "contactName3", contactName3 );
        map.put( "contactPhone3", contactPhone3 );
        RestProxy.getInstance().addContact( map, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                mListener.onConfirm();
                dismiss();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
                dismissProgressDialog();
            }

            @Override
            public void onComplete() {
                dismissProgressDialog();
            }
        } );


    }
}
